package com.fl.sys;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;

import com.fl.util.StringUtil;

/**
 * @author liliang
 * 
 */
public class SysConfig {

	private static Logger log = Logger.getLogger(SysConfig.class);

	private static Scheduler scheduler = null;

	/******************** 常量不需要初始化 ****************************/
	// 数据库部分
	// 参数表名称
	public static final String TALBE_PARAM = "param";
	// 参数表固定字段，作为逻辑主键
	public static final String TALBE_COL_KEY = "key";
	// 参数表固定字段，作为存值字段
	public static final String TALBE_COL_VAL = "value";

	// 数据库KEY值常量
	// 用户名
	public static final String USER = "USER";
	// 密码，MD5加密的
	public static final String PASSWD = "PASSWD";
	// 机器码
	public static final String MAC = "MAC";
	// 注册码
	public static final String LICENSE = "LICENSE";
	// 密钥
	public static final String FTP_SEC_KEY = "FTP_SEC_KEY";
	// 定时上传路径
	public static final String SCHEDULER_PATH = "SCHEDULER_PATH";
	// 定时表达式
	public static final String SCHEDULER_CRON = "SCHEDULER_CRON";
	// ftp端口
	public static final String FTP_PORT = "FTP_PORT";
	// ftp ip
	public static final String FTP_SERVER = "FTP_SERVER";
	// 邮件服务器地址
	public static final String MAIL_SERVER = "MAIL_SERVER";
	// 邮件服务器用户
	public static final String MAIL_USER = "MAIL_USER";
	// 邮件服务器用户密码
	public static final String MAIL_PASSWD = "MAIL_PASSWD";
	// 邮件发送列表，多个人，英文分号分隔
	public static final String MAIL_LIST = "MAIL_LIST";

	// 系统部分
	// 环境变量中取得的程序主目录
	public static final String APP_HOME = "FOUR_L_APP_HOME";

	// access数据库文件名称
	public static final String ACCESS_FILE_NAME = "/4l/data/fl.mdb";
	
	// sqlite数据库文件名称
	public static final String SQLITE_FILE_NAME = "/4l/data/fl.data";

	// log4j配置文件相对路径名称
	public static final String LOG4J_FILE_PATH = "/4l/conf/log4j.properties";

	// quartz配置文件相对路径名称
	public static final String QUARTZ_FILE_PATH = "/4l/conf/quartz.properties";

	// tmp目录
	public static final String TMP_DIR_NAME = "/tmp";

	// 定时任务job名称
	public static final String DEF_JOB_NAME = "flJob";

	// 定时任务trigger名称
	public static final String DEF_TRIGGER_NAME = "flTrigger";

	// 定时任务jobGroup名称
	public static final String DEF_GROUP_NAME = "flGroup";

	/******************** 常量需要初始化 ****************************/
	// 主目录绝对路径，4lAPP目录
	public static String APP_HOME_PATH = "";
	// access文件绝对路径
	public static String ACCESS_FILE_PATH = "";
	// sqlite数据库文件绝对路径
	public static String SQLITE_FILE_PATH ="";
	// tmp目录
	public static String TMP_PATH = "";

	public static void SysInit() throws Exception {

		// 从环境变量里面去的app_home，然后初始化,在界面启动时候调用
		APP_HOME_PATH = System.getProperty(APP_HOME);
		if (StringUtil.isBlank(APP_HOME_PATH)) {
			throw new Exception(
					"Application err ,can not init application home path !");
		}

		// 初始化log4j保证日志输出
		String log4jPropPath = APP_HOME_PATH + LOG4J_FILE_PATH;
		PropertyConfigurator.configure(log4jPropPath);
		log.info("Init log4j sucessful !");

		// 初始化tmp目录路径
		TMP_PATH = APP_HOME_PATH + TMP_DIR_NAME;

		// 初始化access文件路径
		ACCESS_FILE_PATH = APP_HOME_PATH + ACCESS_FILE_NAME;
		log.info("Data file path is " + ACCESS_FILE_PATH + ".");
		
		// 初始化sqlite db文件路径
		SQLITE_FILE_PATH = APP_HOME_PATH + SQLITE_FILE_NAME;
		log.info("Db file path is " + SQLITE_FILE_PATH + ".");

		// 初始化quartz
		String quartzPropPath = APP_HOME_PATH + QUARTZ_FILE_PATH;
		// org.quartz.scheduler.instanceName
		StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(
				quartzPropPath);
		scheduler = stdSchedulerFactory.getScheduler();
		scheduler.start();
		log.info("Scheduler is start.");

	}

	public static Scheduler getSysScheduler() {
		return scheduler;
	}

}
